/**
 * http配置
 */
import axios from "axios";
import store from "@/store";
import { APIENV } from "@/utils/constant";

const instance = axios.create(); // 生成一个axios实例
// 超时时间15s
instance.defaults.timeout = 15000;
// 全局的 axios 默认值
instance.defaults.baseURL = APIENV[process.env.VUE_APP_APIENV] || APIENV.prod;

instance.defaults.headers.post["Content-Type"] =
  "application/json;charset=UTF-8";

// http请求拦截器
instance.interceptors.request.use(
  config => {
    if (store.state.token) {
      config.headers.Authorization = `token ${store.state.token}`;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// http响应拦截器

/* 退出重新登录 */
const logout = msg => {
  // vm.$store.dispatch("logout");
  // vm.$messagebox.alert(msg).then(() => {
  //   vm.$router.replace({
  //     name: "login",
  //     query: { redirect: vm.$router.currentRoute.fullPath }
  //   });
  // });
};

export default instance;
